package com.xxgc.vfc.common.error.center.system;

import com.xxgc.vfc.api.result.Result;
import com.xxgc.vfc.common.error.center.custom.PhoneOrPassException;
import com.xxgc.vfc.common.error.center.custom.UserPhoneAlreadyExistsException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: SJY
 * @Date: 2023/11/14 09:11:02
 * 参数校验异常
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class ParameterException {
    //参数校验的拦截
    @ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public ResponseEntity<Result> handleValidatedException(Exception e) {
        Result<List<String>> result = null;
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            List<String> errorMessages = ex.getBindingResult().getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.toList());
            result = Result.error(HttpStatus.BAD_REQUEST.value(), errorMessages);
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException ex = (ConstraintViolationException) e;
            List<String> errorMessages = ex.getConstraintViolations().stream()
                    .map(ConstraintViolation::getMessage)
                    .collect(Collectors.toList());
            result = Result.error(HttpStatus.BAD_REQUEST.value(), errorMessages);
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            List<String> errorMessages = ex.getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.toList());
            result = Result.error(HttpStatus.BAD_REQUEST.value(), errorMessages);
        }
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }


    //捕获手机号密码错误异常 PhoneOrPassException
    @ExceptionHandler(PhoneOrPassException.class)
    public ResponseEntity<Result> handlePhoneOrPassException(PhoneOrPassException e) {
        //整合错误信息
        List<String> messages = Collections.singletonList(e.getErrorMessage());
        //badRequest 请求400  的body请求提 Result.error结果错误 状态码400 + 错误信息
        return ResponseEntity.badRequest().body(Result.error(400,messages));
    }

    //手机号已被注册
    @ExceptionHandler(UserPhoneAlreadyExistsException.class)
    public ResponseEntity<Result> userPhoneAlreadyExistsException(UserPhoneAlreadyExistsException e) {
        //整合错误信息
        List<String> messages = Collections.singletonList(e.getErrorMessage());
        //badRequest 请求400  的body请求提 Result.error结果错误 状态码400 + 错误信息
        return ResponseEntity.badRequest().body(Result.error(400,messages));
    }




}
